package org.dromara.yuanshidian.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.yuanshidian.domain.Ab02;
import org.springframework.stereotype.Service;
import org.dromara.yuanshidian.domain.bo.Ab02NewBo;
import org.dromara.yuanshidian.domain.vo.Ab02NewVo;
import org.dromara.yuanshidian.domain.Ab02New;
import org.dromara.yuanshidian.mapper.Ab02NewMapper;
import org.dromara.yuanshidian.service.IAb02NewService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 新在职人员信息Service业务层处理
 *
 * @author TzhongTop
 * @date 2025-01-24
 */
@RequiredArgsConstructor
@Service
public class Ab02NewServiceImpl implements IAb02NewService {

    private final Ab02NewMapper baseMapper;

    /**
     * 查询新在职人员信息
     *
     * @param id 主键
     * @return 新在职人员信息
     */
    @Override
    public Ab02NewVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询新在职人员信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 新在职人员信息分页列表
     */
    @Override
    public TableDataInfo<Ab02NewVo> queryPageList(Ab02NewBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Ab02New> lqw = buildQueryWrapper(bo);
        Page<Ab02NewVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的新在职人员信息列表
     *
     * @param bo 查询条件
     * @return 新在职人员信息列表
     */
    @Override
    public List<Ab02NewVo> queryList(Ab02NewBo bo) {
        LambdaQueryWrapper<Ab02New> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Ab02New> buildQueryWrapper(Ab02NewBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Ab02New> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getAaa027()), Ab02New::getAaa027, bo.getAaa027());
        lqw.eq(StringUtils.isNotBlank(bo.getDwh()), Ab02New::getDwh, bo.getDwh());
        lqw.eq(StringUtils.isNotBlank(bo.getGrbh()), Ab02New::getGrbh, bo.getGrbh());
        lqw.eq(bo.getGr00() != null, Ab02New::getGr00, bo.getGr00());
        lqw.eq(StringUtils.isNotBlank(bo.getDw01()), Ab02New::getDw01, bo.getDw01());
        lqw.eq(StringUtils.isNotBlank(bo.getDw02()), Ab02New::getDw02, bo.getDw02());
        lqw.eq(StringUtils.isNotBlank(bo.getBh()), Ab02New::getBh, bo.getBh());
        lqw.eq(bo.getGrid() != null, Ab02New::getGrid, bo.getGrid());
        lqw.eq(StringUtils.isNotBlank(bo.getGr01()), Ab02New::getGr01, bo.getGr01());
        lqw.eq(StringUtils.isNotBlank(bo.getGr02()), Ab02New::getGr02, bo.getGr02());
        lqw.eq(StringUtils.isNotBlank(bo.getGr03()), Ab02New::getGr03, bo.getGr03());
        lqw.eq(StringUtils.isNotBlank(bo.getGr04()), Ab02New::getGr04, bo.getGr04());
        lqw.eq(StringUtils.isNotBlank(bo.getGr05()), Ab02New::getGr05, bo.getGr05());
        lqw.eq(StringUtils.isNotBlank(bo.getGr06()), Ab02New::getGr06, bo.getGr06());
        lqw.eq(bo.getGr07() != null, Ab02New::getGr07, bo.getGr07());
        lqw.eq(bo.getGr08() != null, Ab02New::getGr08, bo.getGr08());
        lqw.eq(StringUtils.isNotBlank(bo.getGr09()), Ab02New::getGr09, bo.getGr09());
        lqw.eq(bo.getGr10() != null, Ab02New::getGr10, bo.getGr10());
        lqw.eq(bo.getGr11a() != null, Ab02New::getGr11a, bo.getGr11a());
        lqw.eq(bo.getGr11b() != null, Ab02New::getGr11b, bo.getGr11b());
        lqw.eq(bo.getGr11() != null, Ab02New::getGr11, bo.getGr11());
        lqw.eq(bo.getGr12() != null, Ab02New::getGr12, bo.getGr12());
        lqw.eq(bo.getGr13() != null, Ab02New::getGr13, bo.getGr13());
        lqw.eq(bo.getGr14() != null, Ab02New::getGr14, bo.getGr14());
        lqw.eq(bo.getGr15() != null, Ab02New::getGr15, bo.getGr15());
        lqw.eq(bo.getGr16() != null, Ab02New::getGr16, bo.getGr16());
        lqw.eq(bo.getGr17() != null, Ab02New::getGr17, bo.getGr17());
        lqw.eq(bo.getGr18() != null, Ab02New::getGr18, bo.getGr18());
        lqw.eq(bo.getGr19() != null, Ab02New::getGr19, bo.getGr19());
        lqw.eq(bo.getGr20() != null, Ab02New::getGr20, bo.getGr20());
        lqw.eq(bo.getGr21() != null, Ab02New::getGr21, bo.getGr21());
        lqw.eq(bo.getGr210() != null, Ab02New::getGr210, bo.getGr210());
        lqw.eq(bo.getGr211() != null, Ab02New::getGr211, bo.getGr211());
        lqw.eq(bo.getGr212() != null, Ab02New::getGr212, bo.getGr212());
        lqw.eq(bo.getGr213() != null, Ab02New::getGr213, bo.getGr213());
        lqw.eq(bo.getGr214() != null, Ab02New::getGr214, bo.getGr214());
        lqw.eq(StringUtils.isNotBlank(bo.getGr22()), Ab02New::getGr22, bo.getGr22());
        lqw.eq(StringUtils.isNotBlank(bo.getGr23()), Ab02New::getGr23, bo.getGr23());
        lqw.eq(StringUtils.isNotBlank(bo.getYd03()), Ab02New::getYd03, bo.getYd03());
        lqw.eq(StringUtils.isNotBlank(bo.getYd08()), Ab02New::getYd08, bo.getYd08());
        lqw.eq(bo.getYd10() != null, Ab02New::getYd10, bo.getYd10());
        lqw.eq(StringUtils.isNotBlank(bo.getYd11()), Ab02New::getYd11, bo.getYd11());
        lqw.eq(bo.getYd12() != null, Ab02New::getYd12, bo.getYd12());
        lqw.eq(bo.getGr24() != null, Ab02New::getGr24, bo.getGr24());
        lqw.eq(StringUtils.isNotBlank(bo.getGr26()), Ab02New::getGr26, bo.getGr26());
        lqw.eq(bo.getGr50() != null, Ab02New::getGr50, bo.getGr50());
        lqw.eq(bo.getGr51() != null, Ab02New::getGr51, bo.getGr51());
        lqw.eq(bo.getGr52() != null, Ab02New::getGr52, bo.getGr52());
        lqw.eq(bo.getGr53() != null, Ab02New::getGr53, bo.getGr53());
        lqw.eq(StringUtils.isNotBlank(bo.getGr55()), Ab02New::getGr55, bo.getGr55());
        lqw.eq(bo.getGr57() != null, Ab02New::getGr57, bo.getGr57());
        lqw.eq(bo.getGr60() != null, Ab02New::getGr60, bo.getGr60());
        lqw.eq(StringUtils.isNotBlank(bo.getEd()), Ab02New::getEd, bo.getEd());
        lqw.eq(StringUtils.isNotBlank(bo.getEdlp()), Ab02New::getEdlp, bo.getEdlp());
        lqw.eq(bo.getEdsj() != null, Ab02New::getEdsj, bo.getEdsj());
        lqw.eq(StringUtils.isNotBlank(bo.getDw24()), Ab02New::getDw24, bo.getDw24());
        lqw.eq(bo.getSystime() != null, Ab02New::getSystime, bo.getSystime());
        lqw.eq(StringUtils.isNotBlank(bo.getTx52()), Ab02New::getTx52, bo.getTx52());
        lqw.eq(StringUtils.isNotBlank(bo.getTx57()), Ab02New::getTx57, bo.getTx57());
        lqw.eq(StringUtils.isNotBlank(bo.getTx58()), Ab02New::getTx58, bo.getTx58());
        lqw.eq(StringUtils.isNotBlank(bo.getPass()), Ab02New::getPass, bo.getPass());
        lqw.eq(StringUtils.isNotBlank(bo.getGr61()), Ab02New::getGr61, bo.getGr61());
        lqw.eq(StringUtils.isNotBlank(bo.getGr25()), Ab02New::getGr25, bo.getGr25());
        lqw.eq(bo.getGr18a() != null, Ab02New::getGr18a, bo.getGr18a());
        lqw.eq(bo.getGr19a() != null, Ab02New::getGr19a, bo.getGr19a());
        lqw.eq(bo.getGr24a() != null, Ab02New::getGr24a, bo.getGr24a());
        lqw.eq(StringUtils.isNotBlank(bo.getSfbj()), Ab02New::getSfbj, bo.getSfbj());
        lqw.eq(StringUtils.isNotBlank(bo.getTzqsy()), Ab02New::getTzqsy, bo.getTzqsy());
        lqw.eq(StringUtils.isNotBlank(bo.getBz()), Ab02New::getBz, bo.getBz());
        lqw.eq(StringUtils.isNotBlank(bo.getTz01()), Ab02New::getTz01, bo.getTz01());
        lqw.eq(bo.getZrAmount() != null, Ab02New::getZrAmount, bo.getZrAmount());
        lqw.eq(bo.getRyType() != null, Ab02New::getRyType, bo.getRyType());
        lqw.eq(bo.getYdType() != null, Ab02New::getYdType, bo.getYdType());
        lqw.eq(bo.getZrStartmonth() != null, Ab02New::getZrStartmonth, bo.getZrStartmonth());
        lqw.eq(bo.getZrEndmonth() != null, Ab02New::getZrEndmonth, bo.getZrEndmonth());
        lqw.eq(bo.getStatus() != null, Ab02New::getStatus, bo.getStatus());
        lqw.eq(bo.getDelStatus() != null, Ab02New::getDelStatus, bo.getDelStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getFileurl()), Ab02New::getFileurl, bo.getFileurl());
        lqw.eq(bo.getRefStatus() != null, Ab02New::getRefStatus, bo.getRefStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getTableflieurl()), Ab02New::getTableflieurl, bo.getTableflieurl());
        return lqw;
    }

    /**
     * 新增新在职人员信息
     *
     * @param bo 新在职人员信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(Ab02NewBo bo) {
        Ab02New add = MapstructUtils.convert(bo, Ab02New.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改新在职人员信息
     *
     * @param bo 新在职人员信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(Ab02NewBo bo) {
        Ab02New update = MapstructUtils.convert(bo, Ab02New.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Ab02New entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除新在职人员信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
